<?php
/**
 * Created by PhpStorm.
 * User: rocky
 * Date: 2020-03-08
 * Time: 16:04
 */

namespace app\common\tools;

class Str
{
    /**
     * 下划线转驼峰
     * @param $uncamelized_words 字符串
     * @param Str $separator 需要转换的字符
     * @return Str
     */
    public static function camelize($uncamelized_words, $separator = '_')
    {
        $uncamelized_words = $separator . str_replace($separator, " ", strtolower($uncamelized_words));
        return ltrim(str_replace(" ", "", ucwords($uncamelized_words)), $separator);
    }

    /**
     * 驼峰转下划线
     * @param $camelCaps 字符串
     * @param Str $separator 转换成的字符
     * @return Str
     */
    public static function uncamelize($camelCaps, $separator = '_')
    {
        return strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . $separator . "$2", $camelCaps));
    }
}